package aorise.com.frame.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

import aorise.com.frame.FsApplication;
import aorise.com.frame.network.ApiService;
import aorise.com.frame.network.BuildConfig;
import aorise.com.frame.network.CustomSubscriberNoDialog;
import aorise.com.frame.network.Result;

import static aorise.com.frame.network.BuildConfig.DELAY_MILLIS;

/**
 * PROJECT_NAME:food_safety_mgt_app.
 * Created by YangXun.
 * Date: 2019/3/21.
 */

public class TimerService extends Service {
    private Timer timer;
    private TimerTask task;

    public TimerService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        doTimeTask();
        System.out.println("Timetask_start");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 开启定时器刷新token
     */
    private void doTimeTask() {
        timer = new Timer();
//        ((FsApplication) getApplication()).setTimer(timer);
        task = new TimerTask() {
            public void run() {
                refreshToken();
            }
        };
        timer.schedule(task, DELAY_MILLIS, DELAY_MILLIS);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (task != null) {
            task.cancel();
        }
        if (timer != null) {
            timer.cancel();
        }
    }

    /**
     * 刷新token
     */
    private void refreshToken() {
        final SharedPreferences sp = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        String username = sp.getString("username", "");
        String token = ((FsApplication) getApplication()).getToken();
        if (!username.equals("")) {
            ApiService.Utils.getApiService(this).refreshToken(token, username)
                    .subscribe(new CustomSubscriberNoDialog<Result<String>>(this) {
                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            System.out.println("Timetask_errorToken");
                        }

                        @Override
                        public void onNext(Result<String> stringResult) {
                            super.onNext(stringResult);
                            if (stringResult.getCode() == BuildConfig.SUCCESS_CODE) {
                                ((FsApplication) getApplication()).setToken(stringResult.getData());
                                String testStr = ((FsApplication) getApplication()).getTestStr();
                                ((FsApplication) getApplication()).setTestStr(testStr + ",1");
                                System.out.println("Timetask_changeToken" + stringResult.getData());
                            }
                        }
                    });
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw null;
    }
}
